package com.qding.park.manager.web.interceptor;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.qding.park.common.web.controller.BaseController;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.smart.common.web.security.SysUserDTO;

public class LoginInterceptor implements HandlerInterceptor {

    protected static final Logger _log = LoggerFactory.getLogger(LoginInterceptor.class);

    private List<String> notInterceptPathList;

    /*
     * (非 Javadoc) Description: DispatcherServlet 之后执行
     * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }

    /*
     * (非 Javadoc) Description:controller之后DispatcherServlet 之前执行
     * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelView) throws Exception {

    }

    /*
     * (非 Javadoc) Description:请求前处理
     * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // 去掉不需拦截的path
        //
        String requestPath = request.getRequestURI();

        // 显示所有用户的请求
        _log.info(request.getRequestURI());

        if (notInterceptPathList != null) {

            // 更精确的定位
            for (String path : notInterceptPathList) {
                if (requestPath.contains(path)) {
                    return true;
                }
            }
        }

        /**
         * 登录与否判断
         */

        //
        // 判断session中是否有visitor
        //
        HttpSession session = request.getSession();
        if (session == null) {
            // 还没有登录
            returnJsonSystemError(request, response, ResponseCodeEnum.SESSION_TIMEOUT.getValue());
            return false;
        }
        SysUserDTO uerInfo = (SysUserDTO) session.getAttribute(BaseController.SESSION_KEY);
        if(uerInfo==null){
         // 还是没有登录
            returnJsonSystemError(request, response,  ResponseCodeEnum.SESSION_TIMEOUT.getValue());
            return false;
        }

        return true;

    }

    protected void returnJsonSystemError(HttpServletRequest request, HttpServletResponse response,
            int errorCode) throws IOException {

        AnswerMsg result = AnswerMsgFactoryImpl.createSessionTimeoutAnserMsg();

        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(JSON.toJSONString(result));
    }

    public List<String> getNotInterceptPathList() {
        return notInterceptPathList;
    }

    public void setNotInterceptPathList(List<String> notInterceptPathList) {
        this.notInterceptPathList = notInterceptPathList;
    }

}
